/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.query.command;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.niclab.vanbus.R;
import org.niclab.vanbus.application.VanBusApplication;
import org.niclab.vanbus.model.BusStop;
import org.niclab.vanbus.model.viewadpater.CommonListAdapter;

import android.content.Context;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ClosestStopsByRouteUICmd implements ICommand{

	private Context ctx;
	private CommonListAdapter model;
	private ListView v;

	public ClosestStopsByRouteUICmd(ListView v,CommonListAdapter model) {
	
		this.ctx = v.getContext();
		this.model = model;
		this.v=v;
	}

	@Override
	public void execute(Object result, ICommandChain chain) {
		
		final List<Integer> positions = (List<Integer>) result;
		
		if(positions.size()==0){
			Context context = ctx.getApplicationContext();
			CharSequence text = "No stops are found close to you !";
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.setDuration(3);
			toast.show();
			
		}else{
			Context context = ctx.getApplicationContext();
			CharSequence text = "Found "+positions.size()+" stops close to you";
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
			
			model.notifyDataSetChanged();
			//scroll to the first closest stop
			v.post(new Runnable(){

				@Override
				public void run() {
					v.setSelection(positions.get(0));
					
				}
				
			});
		
			//clear the closest stop 
			Runnable clear = new Runnable(){

				@Override
				public void run() {
					for(Integer i:positions){
						BusStop stop = (BusStop) model.getItem(i);
						stop.setClosest(false);
						
					}
					
				}
				
			};
			((VanBusApplication) context).getScheduledExecutorService().schedule(clear, Integer.parseInt(context.getString(R.string.bus_stop_closet_invalidate_threshold))*60, TimeUnit.SECONDS);
		}
		
		chain.execute(result);
			
	
		
	}
	

}
